package com.platform.service.impl;

import com.platform.dao.LotteryDao;
import com.platform.dao.UserAssetDao;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.CoinTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.platform.dao.LotteryWagerDao;
import org.springframework.transaction.annotation.Transactional;

/**
 * 彩票购买记录Service实现类
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-05-05 14:25:58
 */
@Service("lotteryWagerService")
public class LotteryWagerServiceImpl implements LotteryWagerService {
    @Autowired
    private LotteryWagerDao lotteryWagerDao;
    @Autowired
    private LotteryDao lotteryDao;
    @Autowired
    private IncomeCoinService incomeCoinService;
    @Autowired
    private EnergySpcService energySpcService;
    @Override
    public LotteryWagerEntity queryObject(Integer id) {
        return lotteryWagerDao.queryObject(id);
    }
    @Autowired
    private UserService userService;
    @Autowired
    private UserAssetDao userAssetDao;

    @Override
    public int  openlottery(LotteryWinningEntity lotteryWinningEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("stages", lotteryWinningEntity.getStages());
        List<LotteryWagerEntity>  lotteryWagerEntitys= lotteryWagerDao.queryList(map);
        map.put("type", 5);//彩票奖励类型
        List<LotteryEntity>  lotterys= lotteryDao.queryList(map);
        LotteryEntity levelone = null;
        LotteryEntity leveltwo = null;
        LotteryEntity levelthree = null;
        LotteryEntity levelfour = null;
        for (LotteryEntity lottery : lotterys) {
            if(lottery.getLevel().equals(1))
                levelone=lottery;
            if(lottery.getLevel().equals(2))
                leveltwo=lottery;
            if(lottery.getLevel().equals(3))
                levelthree=lottery;
            if(lottery.getLevel().equals(4))
                levelfour=lottery;
        }
        String[] lotteryWinningreds = lotteryWinningEntity.getRedBall().split("-");
        for (LotteryWagerEntity lotteryWagerEntity : lotteryWagerEntitys) {
            Integer redballnum = 0;
            String[] lotteryWagerreds = lotteryWagerEntity.getRedBall().split("-");

            for (String lotteryWagerred : lotteryWagerreds) {
                if(Arrays.asList(lotteryWinningreds).contains(lotteryWagerred))
                    redballnum++;
            }
            //一等奖4中
            if(redballnum==4)
                updateassetandlotterywager(lotteryWagerEntity,levelone);
                //一等奖3中
            else if(redballnum==3)
                updateassetandlotterywager(lotteryWagerEntity,leveltwo);
                //一等奖2中
            else if(redballnum==2)
                updateassetandlotterywager(lotteryWagerEntity,levelthree);
                //一等奖1中
            else if(redballnum==1)
                updateassetandlotterywager(lotteryWagerEntity,levelfour);
        }

        /*LotteryEntity levelone = null;
        LotteryEntity leveltwo = null;
        LotteryEntity levelthree = null;
        LotteryEntity levelfour = null;
        LotteryEntity levelfive = null;
        LotteryEntity levelsix = null;
        for (LotteryEntity lottery : lotterys) {
            if(lottery.getLevel().equals(1))
                levelone=lottery;
            if(lottery.getLevel().equals(2))
                leveltwo=lottery;
            if(lottery.getLevel().equals(3))
                levelthree=lottery;
            if(lottery.getLevel().equals(4))
                levelfour=lottery;
            if(lottery.getLevel().equals(5))
                levelfive=lottery;
            if(lottery.getLevel().equals(6))
                levelsix=lottery;
        }

        String[] lotteryWinningreds = lotteryWinningEntity.getRedBall().split("-");
        for (LotteryWagerEntity lotteryWagerEntity : lotteryWagerEntitys) {
            Integer redballnum = 0;Boolean isblueball=false;
            if(lotteryWagerEntity.getBlueBall()==lotteryWinningEntity.getBlueBall())
                isblueball=true;
            String[] lotteryWagerreds = lotteryWagerEntity.getRedBall().split("-");

            for (String lotteryWagerred : lotteryWagerreds) {
                if(Arrays.asList(lotteryWinningreds).contains(lotteryWagerred))
                    redballnum++;
            }
            //一等奖6红1蓝
            if(redballnum==6&&isblueball&levelone!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelone);
            //二等奖6红
            else if(redballnum==6&&!isblueball&leveltwo!=null)
                updateassetandlotterywager(lotteryWagerEntity,leveltwo);
            //三等奖5红1蓝
            else if(redballnum==5&&isblueball&levelthree!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelthree);
            //四等奖5红或者4红1蓝
            else if(redballnum==5&&!isblueball&levelfour!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelfour);
            else if(redballnum==4&&isblueball&levelfour!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelfour);
            //五等奖4红或者3红1蓝
            else if(redballnum==4&&!isblueball&levelfive!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelfive);
            else if(redballnum==3&&isblueball&levelfive!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelfive);
            //六等篮球中
            else if(isblueball&levelsix!=null)
                updateassetandlotterywager(lotteryWagerEntity,levelsix);
        }*/
        return 1;
    }

    @Transactional
    public  boolean updateassetandlotterywager(LotteryWagerEntity lotteryWagerEntity ,LotteryEntity lottery)
    {
        lotteryWagerEntity.setLevel(lottery.getLevel());
        lotteryWagerEntity.setLotteryName(lottery.getLevelName());
        BigDecimal lottercoin = new BigDecimal(lotteryWagerEntity.getNum()+"").multiply( new BigDecimal(lottery.getNumber()+""));
        lotteryWagerEntity.setBonus(lottercoin);
        lotteryWagerEntity.setExchange("1");
        int l = lotteryWagerDao.update(lotteryWagerEntity);
        if (l <= 0) {
            return false;
        }
        UserAssetEntity asset = userAssetDao.queryObjectForUid(lotteryWagerEntity.getUid());
        UserEntity user = userService.queryObject(lotteryWagerEntity.getUid());

        EnergySpcEntity energySpcEntity = new EnergySpcEntity();
        energySpcEntity.setUid(user.getId());
        energySpcEntity.setInviteUid(user.getId());
        energySpcEntity.setPhone(user.getMobile());
        energySpcEntity.setInvitePhone(user.getMobile());
        energySpcEntity.setImage(user.getPictureUrl());
        energySpcEntity.setType(CoinTypeEnum.COIN_TYPE_GAME_IN.getKey());
        energySpcEntity.setIncomeType(0);
        energySpcEntity.setTypeName(CoinTypeEnum.COIN_TYPE_GAME_IN.getValue());
        energySpcEntity.setEnergyCoin(lottercoin.doubleValue());
        energySpcEntity.setCreateDate(new Date());
        energySpcService.save(energySpcEntity);
       /* IncomeCoinEntity ic = IncomeCoinEntity.builder().tradeNo(user.getMobile() + newid).phone(user.getMobile()).uid(user.getId())
                .sportCoin(asset.getSpcCoin().add(lottercoin)).type(CoinTypeEnum.COIN_TYPE_JOB.getKey()).statu(1).build();
        incomeCoinService.save(ic);*/
        asset.setSpcCoin(asset.getSpcCoin().add(lottercoin));
        int i = userAssetDao.updateAsset(asset);
        if (i <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public List<LotteryWagerEntity> queryList(Map<String, Object> map) {
        return lotteryWagerDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return lotteryWagerDao.queryTotal(map);
    }

    @Override
    public int save(LotteryWagerEntity lotteryWager) {
        return lotteryWagerDao.save(lotteryWager);
    }

    @Override
    public int update(LotteryWagerEntity lotteryWager) {
        return lotteryWagerDao.update(lotteryWager);
    }

    @Override
    public int delete(Integer id) {
        return lotteryWagerDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return lotteryWagerDao.deleteBatch(ids);
    }
}
